import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Attention } from "src/entities/attention.entity";
import { User } from "src/entities/user.entity";
import { AttentionInput, AttentionSortInput, AttentionWhereInput, LabelInput, PageInput } from "src/graphql";
import { AttentionService } from "src/services/attention/attention.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Attention')
export class AttentionResolver {
  constructor(
    private readonly attentionService: AttentionService,
    private readonly userService: UserService
  ) {}

  @Query('attention')
  async getAttentionById(@Args() id: number) {
    return await this.attentionService.findOne(id);
  }

  @Query('attentions')
  async getAttentions(@Args('page') page: PageInput,@Args('sort') sort:AttentionSortInput,@Args('where') where:AttentionWhereInput) {
    return await this.attentionService.getPagination(page,sort,where);
  }

  @Mutation()
  async addAttention(@Args('attention') attention:AttentionInput) {
    return await this.attentionService.addOne(<Attention>attention);
  }

  @Mutation()
  async removeAttention(@Args() id: number) {
    return await this.attentionService.removeOne(id);
  }

  @Mutation()
  async modifyAttention(@Args('id') id: number, @Args('attention') attention:AttentionInput) {
    return await this.attentionService.modifyOne(id,<Attention>attention);
  }

  @ResolveProperty(() => User,{
    name:'_fromUser'
  })
  async fromUser(@Parent() attention){
    const { from_uid } = attention;
    return await this.userService.findOne(from_uid);
  }

  @ResolveProperty(() => User,{
    name:'_toUser'
  })
  async toUser(@Parent() attention){
    const { to_uid } = attention;
    return await this.userService.findOne(to_uid);
  }
}